<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input v-model="formItem.code" placeholder="请输入编码" />
      </el-form-item>
      <el-form-item label="工单类型" prop="workType">
        <el-select v-model="formItem.workType" placeholder="请选择工单类型">
          <el-option v-for="(item) in workTypeList" :key="item.code" :label="item.text" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="表单类型" prop="type">
        <el-select v-model="formItem.type" placeholder="请选择表单类型">
          <el-option v-for="(item) in typeList" :key="item.code" :label="item.name" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="表单详情">
        <el-button icon="el-icon-plus" @click="add">添加</el-button>
      </el-form-item>
      <el-row v-for="(item, index) in formItem.items" :key="index" :gutter="20">
        <el-col :span="10">
          <el-form-item :ref="index + 'name'" label="名称" :prop="'items.' + index +'.name'" :rules="{required: true, message: '请输入字段名称', trigger: 'blur'}">
            <el-input v-model="item.name" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item :ref="index + 'nodeKey'" label="表单字段KEY" :prop="'items.' + index + '.nodeKey'" :rules="{required: true, message: '请输入字段KEY', trigger: 'blur'}">
            <el-input v-model="item.nodeKey" placeholder="请输入字段KEY" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-tooltip class="item" effect="light" content="删除" placement="right">
            <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="remove(index)" />
          </el-tooltip>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { list } from '@neu/dcam-ui/api/dictionary'
import {
  create,
  update,
  detail
} from '@neu/dcm-ui/api/workflowForm'

// 定义一个表单原始项
const formItem = {
  name: '',
  code: '',
  items: [],
  type: 'other',
  workType: ''
}
export default {
  name: 'WorkflowFormEditForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 类型下拉
      typeList: [
        {
          code: 'other',
          name: '普通表单'
        },
        {
          code: 'startForm',
          name: '开始表单'
        }
      ],
      // 工单类型集合
      workTypeList: [],
      // 表单验证规则
      rules: {
        name: [{
          required: true,
          message: '表单名称不能为空',
          trigger: 'blur'
        }],
        workType: [{
          required: true,
          message: '工单类型不能为空',
          trigger: 'blur'
        }],
        type: [{
          required: true,
          message: '类型不能为空',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '编码不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    list({ s_type: 'workorderType' }).then(res => { this.workTypeList = res.data })
    this.fetch()
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    // 添加一行
    add() {
      this.formItem.items.push({ name: '', nodeKey: '' })
    },
    // 移除信息
    remove(index) {
      this.formItem.items.splice(index, 1)
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem.items = []
    }
  }
}
</script>
